<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='global-property-S-'>/**
</span> * @fileOverview 验证规则
 * @ignore
 */

var $ = require(&#39;jquery&#39;),
  BUI = require(&#39;bui-common&#39;);
<span id='BUI-Form-Rule'>/**
</span> * @class BUI.Form.Rule
 * 验证规则
 * @extends BUI.Base
 */
var Rule = function (config){
  Rule.superclass.constructor.call(this,config);
}

BUI.extend(Rule,BUI.Base);

Rule.ATTRS = {
<span id='BUI-Form-Rule-property-name'>  /**
</span>   * 规则名称
   * @type {String}
   */
  name : {

  },
<span id='BUI-Form-Rule-property-msg'>  /**
</span>   * 验证失败信息
   * @type {String}
   */
  msg : {

  },
<span id='BUI-Form-Rule-property-validator'>  /**
</span>   * 验证函数
   * @type {Function}
   */
  validator : {
    value : function(value,baseValue,formatedMsg,control){

    }
  }
}

//是否通过验证
function valid(self,value,baseValue,msg,control){
  if(BUI.isArray(baseValue) &amp;&amp; BUI.isString(baseValue[1])){
    if(baseValue[1]){
      msg = baseValue[1];
    }
    baseValue = baseValue[0];
  }
  var _self = self,
    validator = _self.get(&#39;validator&#39;),
    formatedMsg = formatError(self,baseValue,msg),
    valid = true;
  value = value == null ? &#39;&#39; : value;
  return validator.call(_self,value,baseValue,formatedMsg,control);
}

function parseParams(values){

  if(values == null){
    return {};
  }

  if($.isPlainObject(values)){
    return values;
  }

  var ars = values,
      rst = {};
  if(BUI.isArray(values)){

    for(var i = 0; i &lt; ars.length; i++){
      rst[i] = ars[i];
    }
    return rst;
  }

  return {&#39;0&#39; : values};
}

function formatError(self,values,msg){
  var ars = parseParams(values); 
  msg = msg || self.get(&#39;msg&#39;);
  return BUI.substitute(msg,ars);
}

BUI.augment(Rule,{

<span id='BUI-Form-Rule-method-valid'>  /**
</span>   * 是否通过验证，该函数可以接收多个参数
   * @param  {*}  [value] 验证的值
   * @param  {*} [baseValue] 跟传入值相比较的值
   * @param {String} [msg] 验证失败后的错误信息，显示的错误中可以显示 baseValue中的信息
   * @param {BUI.Form.Field|BUI.Form.Group} [control] 发生验证的控件
   * @return {String}   通过验证返回 null ,未通过验证返回错误信息
   * 
   *         var msg = &#39;输入数据必须在{0}和{1}之间！&#39;,
   *           rangeRule = new Rule({
   *             name : &#39;range&#39;,
   *             msg : msg,
   *             validator :function(value,range,msg){
   *               var min = range[0], //此处我们把range定义为数组，也可以定义为{min:0,max:200},那么在传入校验时跟此处一致即可
   *                 max = range[1];   //在错误信息中，使用用 &#39;输入数据必须在{min}和{max}之间！&#39;,验证函数中的字符串已经进行格式化
   *               if(value &lt; min || value &gt; max){
   *                 return false;
   *               }
   *               return true;
   *             }
   *           });
   *         var range = [0,200],
   *           val = 100,
   *           error = rangeRule.valid(val,range);//msg可以在此处重新传入
   *         
   */
  valid : function(value,baseValue,msg,control){
    var _self = this;
    return valid(_self,value,baseValue,msg,control);
  }
});

module.exports = Rule;
</pre>
</body>
</html>
